package com.ruoyi.order.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.s7.ST30A.ST30AVo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.order.bo.ProductInfo;
import com.ruoyi.order.domain.MesProductNodeResult;
import com.ruoyi.order.enums.NodeTestResultEnum;
import com.ruoyi.order.service.IMesProductNodeResultService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.order.mapper.MesOrderProductMapper;
import com.ruoyi.order.domain.MesOrderProduct;
import com.ruoyi.order.service.IMesOrderProductService;

/**
 * 产品列Service业务层处理
 *
 * @author guoxl
 * @date 2025-04-03
 */
@Service
public class MesOrderProductServiceImpl implements IMesOrderProductService {
    @Autowired
    private MesOrderProductMapper mesOrderProductMapper;

    @Autowired
    private IMesProductNodeResultService mesProductNodeResultService;

    /**
     * 查询产品列
     *
     * @param productId 产品列主键
     * @return 产品列
     */
    @Override
    public MesOrderProduct selectMesOrderProductByProductId(Long productId) {
        return mesOrderProductMapper.selectMesOrderProductByProductId(productId);
    }

    @Override
    public ProductInfo selectMesOrderProductByPartNr(String partNr) {
        MesOrderProduct mesOrderProduct = mesOrderProductMapper.selectMesOrderProductByPartNr(partNr);
        if (mesOrderProduct != null) {
            ProductInfo productInfo = new ProductInfo();
            productInfo.setPartNumber(mesOrderProduct.getPartNumber());
            productInfo.setModel(mesOrderProduct.getModel());
            productInfo.setOutModel(mesOrderProduct.getOutModel());
            ST30AVo st30AVo = new ST30AVo();
            MesProductNodeResult parameter = new MesProductNodeResult();
            parameter.setPartNumber(partNr);
            List<MesProductNodeResult> mesProductNodeResultList = mesProductNodeResultService.selectMesProductNodeResultByPartNumber(parameter);
            if (!CollectionUtils.isNotEmpty(mesProductNodeResultList)) {
                for (MesProductNodeResult item : mesProductNodeResultList) {
                    if (item.getStationCode().equals("ST30A")) {
                        st30AVo = JSON.parseObject(item.getTestDetail(), ST30AVo.class);
                        productInfo.setCheckLeftOrRightArrow(st30AVo.getCheckLeftOrRightArrow());
                    }
                }
            }
            return productInfo;
        }
        return null;
    }

    /**
     * 查询产品列
     *
     * @param managementNum 设备编号
     * @return 产品列
     */
    @Override
    public MesOrderProduct selectByManagementNum(String managementNum) {
        return mesOrderProductMapper.selectByManagementNum(managementNum);
    }

    /**
     * 查询产品列列表
     *
     * @param mesOrderProduct 产品列
     * @return 产品列
     */
    @Override
    public List<MesOrderProduct> selectMesOrderProductList(MesOrderProduct mesOrderProduct) {
        return mesOrderProductMapper.selectMesOrderProductList(mesOrderProduct);
    }

    /**
     * 新增产品列
     *
     * @param mesOrderProduct 产品列
     * @return 结果
     */
    @Override
    public int insertMesOrderProduct(MesOrderProduct mesOrderProduct) {
        mesOrderProduct.setCreateTime(DateUtils.getNowDate());
        return mesOrderProductMapper.insertMesOrderProduct(mesOrderProduct);
    }

    /**
     * 修改产品列
     *
     * @param mesOrderProduct 产品列
     * @return 结果
     */
    @Override
    public int updateMesOrderProduct(MesOrderProduct mesOrderProduct) {
        mesOrderProduct.setUpdateTime(DateUtils.getNowDate());
        return mesOrderProductMapper.updateMesOrderProduct(mesOrderProduct);
    }

    /**
     * 批量删除产品列
     *
     * @param productIds 需要删除的产品列主键
     * @return 结果
     */
    @Override
    public int deleteMesOrderProductByProductIds(Long[] productIds) {
        return mesOrderProductMapper.deleteMesOrderProductByProductIds(productIds);
    }

    /**
     * 删除产品列信息
     *
     * @param productId 产品列主键
     * @return 结果
     */
    @Override
    public int deleteMesOrderProductByProductId(Long productId) {
        return mesOrderProductMapper.deleteMesOrderProductByProductId(productId);
    }
}
